﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class FlowDiagram : BasePage
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //clear all hidden fields
                this.hfStation.Value = string.Empty;
                this.hfDevices.Value = string.Empty;
                this.hfIncomingStation.Value = string.Empty;
                this.hfOutgoingStation.Value = string.Empty;
                
                var flows = this.DbContext.VIBA_LUONGs.ToList();
                var stations = this.DbContext.VIBA_TRAMs.ToList();

                Dictionary<string, string> list = new Dictionary<string,string>();
                for (int i = 0; i < flows.Count; i++)
                {
                    if (!list.Keys.Contains(flows[i].Luong_Id_Tram_Di.Trim()))
                    {
                        var station = stations.FirstOrDefault(x => x.Tram_Id.Trim() == flows[i].Luong_Id_Tram_Di.Trim());
                        if (station != null)
                        {
                            list.Add(station.Tram_Id.Trim(), station.Tram_Ten);
                            var root = new RadTreeNode(station.Tram_Ten, station.Tram_Id.Trim());
                            var stationOutgoing = stations.FirstOrDefault(x => x.Tram_Id.Trim() == flows[i].Luong_Id_Tram_Den.Trim());
                            if (stationOutgoing != null)
                            {
                                root.Nodes.Add(new RadTreeNode(stationOutgoing.Tram_Ten, stationOutgoing.Tram_Id));
                            }
                            this.tree.Nodes.Add(root);
                            if (i + 1 < flows.Count)
                            {
                                for (int j = i + 1; j < flows.Count; j++)
                                {
                                    if (flows[j].Luong_Id_Tram_Di.Trim() == flows[i].Luong_Id_Tram_Di.Trim())
                                    {
                                        var station2 = stations.FirstOrDefault(x => x.Tram_Id == flows[j].Luong_Id_Tram_Den.Trim());
                                        root.Nodes.Add(new RadTreeNode(station2.Tram_Ten, station2.Tram_Id.Trim()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void tree_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            //clear all hidden fields
            this.hfStation.Value = string.Empty;
            this.hfDevices.Value = string.Empty;
            this.hfIncomingStation.Value = string.Empty;
            this.hfOutgoingStation.Value = string.Empty;

            string stationId = e.Node.Value;
            var station = this.DbContext.VIBA_TRAMs.FirstOrDefault(x => x.Tram_Id == stationId);
            if (station != null)
            {
                this.hfStation.Value = station.Tram_Ten;
                var devices = this.DbContext.VIBA_THIET_BIs.Where(x => x.Tram_Id == stationId).ToList();
                if (devices != null && devices.Count > 0)
                {
                    bool haveIncomings = false;
                    bool haveOutgoings = false;
                    for (int i = 0; i < devices.Count; i++)
                    {
                        this.hfDevices.Value += devices[i].ThietBi_Ten + "(" + GetSoPort(devices[i].ThietBi_Id) + ")" + "|";

                        //=========== get incoming stations =================
                        {
                            var flows = this.DbContext.VIBA_LUONGs.Where(x => x.Luong_Port_Den == GetSoPort(devices[i].ThietBi_Id)).ToList();
                            if (flows != null && flows.Count > 0)
                            {
                                haveIncomings = true;
                                List<string> list = new List<string>();
                                for (int j = 0; j < flows.Count; j++)
                                    list.Add(flows[j].Luong_Id_Tram_Di.Trim());
                                var inStations = this.DbContext.VIBA_TRAMs.Where(x => list.Contains(x.Tram_Id.Trim())).ToList();
                                if (inStations != null && inStations.Count > 0)
                                {
                                    for (int j = 0; j < inStations.Count; j++)
                                        if (inStations[j].Tram_Id.Trim() != station.Tram_Id.Trim())
                                            this.hfIncomingStation.Value += inStations[j].Tram_Ten + "@";
                                    this.hfIncomingStation.Value = this.hfIncomingStation.Value.Trim('@') + "|";
                                }

                            }
                        }


                        //get outgoing stations
                        {
                            var flows = this.DbContext.VIBA_LUONGs.Where(x => x.Luong_Port_Di == GetSoPort(devices[i].ThietBi_Id)).ToList();
                            if (flows != null && flows.Count > 0)
                            {
                                haveOutgoings = true;
                                List<string> list = new List<string>();
                                for (int j = 0; j < flows.Count; j++)
                                    list.Add(flows[j].Luong_Id_Tram_Den.Trim());
                                var outStations = this.DbContext.VIBA_TRAMs.Where(x => list.Contains(x.Tram_Id.Trim())).ToList();
                                if (outStations != null && outStations.Count > 0)
                                {
                                    for (int j = 0; j < outStations.Count; j++)
                                        if (outStations[j].Tram_Id.Trim() != station.Tram_Id.Trim())
                                            this.hfOutgoingStation.Value += outStations[j].Tram_Ten + "@";
                                    this.hfOutgoingStation.Value = this.hfOutgoingStation.Value.Trim('@') + "|";
                                }

                            }
                        }

                        //process to balance
                        if (haveIncomings && !haveOutgoings)
                            this.hfOutgoingStation.Value += " |";
                        if (haveOutgoings && !haveIncomings)
                            this.hfIncomingStation.Value += " |";

                        //reset
                        haveIncomings = false;
                        haveOutgoings = false;
                    }
                    this.hfDevices.Value = this.hfDevices.Value.Trim('|');
                    this.hfIncomingStation.Value = this.hfIncomingStation.Value.Trim('|');
                    this.hfOutgoingStation.Value = this.hfOutgoingStation.Value.Trim('|');
                }


            }


        }

        protected void tree_NodeClick2(object sender, RadTreeNodeEventArgs e)
        {
            //clear all hidden fields
            this.hfStation.Value = string.Empty;
            this.hfDevices.Value = string.Empty;
            this.hfCards.Value = string.Empty;
            this.hfIncomingStation.Value = string.Empty;
            this.hfOutgoingStation.Value = string.Empty;

            string stationId = e.Node.Value;

            var station = this.DbContext.VIBA_TRAMs.FirstOrDefault(x => x.Tram_Id == stationId);
            if (station != null)
            {
                this.hfStation.Value = station.Tram_Ten;

                var cardIdList1 = this.DbContext.VIBA_LUONGs
                    .Where(x => x.Luong_Id_Tram_Di == stationId).Select(x => x.Luong_Card_Di);
                var cardIdList2 = this.DbContext.VIBA_LUONGs
                    .Where(x => x.Luong_Id_Tram_Den == stationId).Select(x => x.Luong_Card_Den);
                var cardIdList = cardIdList1.Union(cardIdList2).Distinct().ToList();

                //this.NormalizeList(cardIdList);

                if (cardIdList != null && cardIdList.Count > 0)
                {
                    bool haveIncomings = false;
                    bool haveOutgoings = false;
                    for (int i = 0; i < cardIdList.Count; i++)
                    {
                        this.hfCards.Value += GetCardName(cardIdList[i].Value.ToString()) + "(" + GetDeviceName(cardIdList[i].Value) + ")" + "|";

                        //=========== get incoming stations =================
                        {
                            var incomeStationIdList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList[i].Value)
                                .Select(x => x.Luong_Id_Tram_Di).Distinct().ToList();

                            NormalizeList(incomeStationIdList);

                            if (incomeStationIdList != null && incomeStationIdList.Count > 0)
                            {
                                haveIncomings = true;

                                var inStations = this.GetStationNames(incomeStationIdList);
                                if (inStations != null && inStations.Count > 0)
                                {
                                    for (int j = 0; j < inStations.Count; j++)
                                        this.hfIncomingStation.Value += inStations[j] + "@";
                                    this.hfIncomingStation.Value = this.hfIncomingStation.Value.Trim('@') + "|";
                                }
                            }
                        }


                        //============= get outgoing stations =================
                        {
                            var outgoingStationIdList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList[i])
                                .Select(x => x.Luong_Id_Tram_Den).Distinct().ToList();

                            NormalizeList(outgoingStationIdList);

                            if (outgoingStationIdList != null && outgoingStationIdList.Count > 0)
                            {
                                haveOutgoings = true;

                                var outStations = this.GetStationNames(outgoingStationIdList);
                                if (outStations != null && outStations.Count > 0)
                                {
                                    for (int j = 0; j < outStations.Count; j++)
                                        this.hfOutgoingStation.Value += outStations[j] + "@";
                                    this.hfOutgoingStation.Value = this.hfOutgoingStation.Value.Trim('@') + "|";
                                }
                            }
                        }

                        //process to balance
                        if (haveIncomings && !haveOutgoings)
                            this.hfOutgoingStation.Value += " |";
                        if (haveOutgoings && !haveIncomings)
                            this.hfIncomingStation.Value += " |";

                        //reset
                        haveIncomings = false;
                        haveOutgoings = false;
                    }
                    this.hfDevices.Value = this.hfDevices.Value.Trim('|');
                    this.hfCards.Value = this.hfCards.Value.Trim('|');
                    this.hfIncomingStation.Value = this.hfIncomingStation.Value.Trim('|');
                    this.hfOutgoingStation.Value = this.hfOutgoingStation.Value.Trim('|');
                }


            }


        }
        //Nha create - 27/7/2012
        protected void tree_NodeClick3(object sender, RadTreeNodeEventArgs e)
        {
            //clear all hidden fields
            this.hfStation.Value = string.Empty;
            this.hfDevices.Value = string.Empty;
            this.hfCards.Value = string.Empty;
            this.hfCardsIncoming.Value = string.Empty;
            this.hfCardsOutcoming.Value = string.Empty;
            this.hfCardsCenterIn.Value = string.Empty;
            this.hfCardsCenterOut.Value = string.Empty;
            this.hfIncomingStation.Value = string.Empty;
            this.hfOutgoingStation.Value = string.Empty;

            this.hflag.Value ="0"; 

            string stationId = e.Node.Value;
            var station = this.DbContext.VIBA_TRAMs.FirstOrDefault(x => x.Tram_Id == stationId);
            if (station != null)
            {
                this.hfStation.Value = station.Tram_Ten;
                //danh sach card gốc
                var cardIdList1 = this.DbContext.VIBA_LUONGs
                    .Where(x => x.Luong_Id_Tram_Den == stationId && x.Status==1).Select(x => x.Luong_Card_Di).Distinct().ToList();
                //danh sach card ngọn
                var cardIdList2 = this.DbContext.VIBA_LUONGs
                    .Where(x => x.Luong_Id_Tram_Di == stationId && x.Status==1).Select(x => x.Luong_Card_Den).Distinct().ToList();
                var cardIdList = cardIdList1.Union(cardIdList2).Distinct().ToList();

                //truong hop vua co ca tram goc, vua co ca tram ngon
                if (cardIdList1 != null && cardIdList1.Count>0 && cardIdList2 != null && cardIdList2.Count>0)
                {
                    hflag.Value = "2"; // ton tai tram goc va tram ngon
                    for (int i = 0; i < cardIdList1.Count; i++)
                    {
                        //card goc
                        var incomeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Di).Distinct().ToList();
                        //lay thong tin port goc
                        var parentportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Di).Distinct().ToList();
                        //card ngon của tram chon
                        var inCenterCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Den).Distinct().ToList();
                        // thong tin port trung tam(tram chon)
                        var InCenterPortList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Den).Distinct().ToList();
                        // thong tin luong trung tam (tram chon)
                        var InCenterFlowList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Ten).Distinct().ToList();
                        if (incomeCardList != null && incomeCardList.Count > 0)
                        {
                            List<string> InCardList = new List<string>();
                            for (int j = 0; j < incomeCardList.Count; j++)
                            {
                                InCardList.Add(incomeCardList[j].ToString());
                            }
                            var inCards = this.GetCardNames(InCardList);
                            NormalizeList(inCards);
                            if (inCards != null && inCards.Count > 0)
                            {
                                for (int k = 0; k < inCards.Count; k++)
                                    if (parentportList != null)
                                    {
                                        this.hfCardsIncoming.Value += "Card:" + inCards[k] + "-Port:" + parentportList[k] + "@";
                                    }
                                    else
                                    {
                                        this.hfCardsIncoming.Value += "Card:" + inCards[k] + "-Port:" + "@";
                                    }
                                this.hfCardsIncoming.Value = this.hfCardsIncoming.Value.Trim('@') + "|";
                            }
                            var incomeStationIdList = this.DbContext.VIBA_LUONGs
                                        .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                                        .Select(x => x.Luong_Id_Tram_Di).Distinct().ToList();
                            NormalizeList(incomeStationIdList);
                            if (incomeStationIdList != null && incomeStationIdList.Count > 0)
                            {                                
                                var inStations = this.GetStationNames(incomeStationIdList);
                                if (inStations != null && inStations.Count > 0)
                                {
                                    for (int j = 0; j < inStations.Count; j++)
                                        this.hfIncomingStation.Value += inStations[j] + "@";
                                }
                            }
                            List<string> _InCenterCard = new List<string>();
                            List<string> _InCenterPort = new List<string>();
                            for(int j = 0;j<inCenterCardList.Count; j ++ )
                            {
                                _InCenterCard.Add(inCenterCardList[j].ToString());                                
                            }
                            for (int j = 0; j < InCenterPortList.Count; j++)
                            {
                                _InCenterPort.Add(InCenterPortList[j].ToString());
                            }
                            var InCenterCard = this.GetCardNames(_InCenterCard);
                            for (int j = 0; j < InCenterCard.Count; j++)
                            {
                                hfCardsCenterIn.Value += "Card:" + InCenterCard[j] + "-Port:" + _InCenterPort[j]+"-Luong:"+InCenterFlowList[j].ToString() + "@";
                            }
                        } 
                    }
                    this.hfCardsCenterIn.Value = this.hfCardsCenterIn.Value.Trim('|');
                    this.hfCardsIncoming.Value = this.hfCardsIncoming.Value.Trim('|');
                    this.hfIncomingStation.Value = this.hfIncomingStation.Value.Trim('|');
                    for (int i = 0; i < cardIdList2.Count; i++)
                    {
                        //card ngon
                        var outcomeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Den).Distinct().ToList();
                        //lay thong tin so port ngon
                        var childportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Den).Distinct().ToList();
                        //lay thong tin ten luong
                        var nameflowList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Ten).Distinct().ToList();
                        // card goc(tram chon)
                        var OutCenterCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Di).Distinct().ToList();
                        //thong tin so port goc(tram chon)
                        var OutCenterPortList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Di).Distinct().ToList();
                        List<string> OutCardList = new List<string>();
                        for (int j = 0; j < outcomeCardList.Count; j++)
                        {
                            OutCardList.Add(outcomeCardList[j].ToString());
                        }
                        var outCards = this.GetCardNames(OutCardList);
                        NormalizeList(outCards);
                        if (outCards != null && outCards.Count > 0)
                        {
                            for (int k = 0; k < outCards.Count; k++)
                                if (childportList != null && nameflowList != null)
                                {
                                    this.hfCardsOutcoming.Value += "Luong:" + nameflowList[k] + "-Card:" + outCards[k] + "-Port:" + childportList[k] + "@";
                                }
                                else
                                {
                                    this.hfCardsOutcoming.Value += "Luong:" + "-Card:" + outCards[k] + "-Port:" + "@";
                                }
                        }
                        //tram den
                        var outgoingStationIdList = this.DbContext.VIBA_LUONGs
                                    .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i] && x.Status == 1)
                                    .Select(x => x.Luong_Id_Tram_Den).Distinct().ToList();
                        NormalizeList(outgoingStationIdList);
                        if (outgoingStationIdList != null && outgoingStationIdList.Count > 0)
                        {
                            var outStations = this.GetStationNames(outgoingStationIdList);
                            if (outStations != null && outStations.Count > 0)
                            {
                                for (int j = 0; j < outStations.Count; j++)
                                    this.hfOutgoingStation.Value += outStations[j] + "@";
                            }
                        }
                        List<string> _OutCenterCard = new List<string>();
                        for (int j = 0; j < OutCenterCardList.Count; j++)
                        {
                            _OutCenterCard.Add(OutCenterCardList[j].ToString());
                        }
                        var OutCenterCard = this.GetCardNames(_OutCenterCard);
                        for (int j = 0; j < OutCenterCard.Count; j++)
                        {
                            hfCardsCenterOut.Value += "Card:" + OutCenterCard[j] + "-Port:" + OutCenterPortList[j].Value + "@";
                        }
                    }
                    this.hfCardsCenterOut.Value = this.hfCardsCenterOut.Value.Trim('|');
                    this.hfCardsOutcoming.Value = this.hfCardsIncoming.Value.Trim('|');
                    this.hfOutgoingStation.Value = this.hfIncomingStation.Value.Trim('|');
                }
                //-----------------------------********----------------------------------
                

                if (cardIdList1 == null)
                {
                    cardIdList1 = this.DbContext.VIBA_LUONGs
                        .Where(x => x.Luong_Id_Tram_Di == stationId && x.Status==1).Select(x => x.Luong_Card_Di).Distinct().ToList();
                    hflag.Value = "1";//chinh la tram goc
                }
                if (cardIdList1.Count == 0)
                {
                    cardIdList1 = this.DbContext.VIBA_LUONGs
                                           .Where(x => x.Luong_Id_Tram_Di == stationId && x.Status==1).Select(x => x.Luong_Card_Di).Distinct().ToList();
                    hflag.Value = "1";//chinh la tram goc
                }                
                //card goc
                if (cardIdList1 != null && cardIdList1.Count > 0 && hflag.Value != "2")
                {
                    bool haveIncomings = false;
                    bool haveIncomeCard = false;
                    for (int i = 0; i < cardIdList1.Count; i++)
                    {
                        if (cardIdList1[i].HasValue != false)
                        {
                            //card goc
                            var incomeCardList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Card_Di).Distinct().ToList();
                            //lay thong tin port goc
                            var parentportList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Port_Di).Distinct().ToList();                           
                            if (hflag.Value == "0") // truong hop khong phai la tram goc
                            {
                                incomeCardList = this.DbContext.VIBA_LUONGs
                                    .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Card_Di).Distinct().ToList();
                                //lay thong tin port goc
                                parentportList = this.DbContext.VIBA_LUONGs
                                    .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Port_Di).Distinct().ToList();                                
                            }
                            if (incomeCardList != null && incomeCardList.Count > 0)
                            {
                                haveIncomeCard = true;
                                List<string> InCardList = new List<string>();
                                for (int j = 0; j < incomeCardList.Count; j++)
                                {
                                    InCardList.Add(incomeCardList[j].ToString());
                                }
                                var inCards = this.GetCardNames(InCardList);
                                NormalizeList(inCards);
                                if (inCards != null && inCards.Count > 0)
                                {
                                    for (int k = 0; k < inCards.Count; k++)
                                        if (parentportList != null)
                                        {
                                            this.hfCardsIncoming.Value += "Card:" + inCards[k] + "-Port:" + parentportList[k] + "@";
                                        }
                                        else
                                        {
                                            this.hfCardsIncoming.Value += "Card:" + inCards[k] + "-Port:" + "@";
                                        }

                                    this.hfCardsIncoming.Value = this.hfCardsIncoming.Value.Trim('@') + "|";
                                }
                            }
                            //tram goc

                            var incomeStationIdList = this.DbContext.VIBA_LUONGs
                                        .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Di == cardIdList1[i].Value && x.Status == 1)
                                        .Select(x => x.Luong_Id_Tram_Di).Distinct().ToList();

                            NormalizeList(incomeStationIdList);

                            if (incomeStationIdList != null && incomeStationIdList.Count > 0)
                            {
                                haveIncomings = true;

                                var inStations = this.GetStationNames(incomeStationIdList);
                                if (inStations != null && inStations.Count > 0)
                                {
                                    for (int j = 0; j < inStations.Count; j++)
                                        this.hfIncomingStation.Value += inStations[j] + "@";                                    
                                }
                            }
                        }                        
                    }
                    this.hfDevices.Value = this.hfDevices.Value.Trim('|');
                    this.hfCards.Value = this.hfCards.Value.Trim('|');
                    this.hfCardsIncoming.Value = this.hfCardsIncoming.Value.Trim('|');
                    this.hfIncomingStation.Value = this.hfIncomingStation.Value.Trim('|');                    
                }
                



                if (cardIdList2 == null)
                {
                    cardIdList2 = this.DbContext.VIBA_LUONGs
                        .Where(x => x.Luong_Id_Tram_Den == stationId && x.Status==1).Select(x => x.Luong_Card_Den).Distinct().ToList();
                    //hflag.Value = "1";
                }
                if (cardIdList2.Count == 0)
                {
                    cardIdList2 = this.DbContext.VIBA_LUONGs
                        .Where(x => x.Luong_Id_Tram_Den == stationId && x.Status==1).Select(x => x.Luong_Card_Den).Distinct().ToList();
                    
                }

                //card ngon
                if (cardIdList2 != null && cardIdList2.Count > 0 && hflag.Value != "2")
                {
                    bool haveOutgoings = false;
                    bool haveOutcomeCard = false;
                    for (int i = 0; i < cardIdList2.Count; i++)
                    {
                       if (cardIdList2[i].HasValue != false)
                        {
                            //card ngon
                            var outcomeCardList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Card_Den).Distinct().ToList();
                            //lay thong tin so port ngon
                            var childportList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Port_Den).Distinct().ToList();
                            //lay thong tin ten luong
                            var nameflowList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Ten).Distinct().ToList();
                            if (hflag.Value == "1")
                            {
                                outcomeCardList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Card_Den).Distinct().ToList();
                                //lay thong tin so port ngon
                                childportList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Port_Den).Distinct().ToList();
                                //lay thong tin ten luong
                                nameflowList = this.DbContext.VIBA_LUONGs
                                .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i].Value && x.Status == 1)
                                .Select(x => x.Luong_Ten).Distinct().ToList();
                            }
                            if (outcomeCardList != null && outcomeCardList.Count > 0)
                            {
                                haveOutcomeCard = true;
                                List<string> OutCardList = new List<string>();
                                for (int j = 0; j < outcomeCardList.Count; j++)
                                {
                                    OutCardList.Add(outcomeCardList[j].ToString());
                                }
                                var outCards = this.GetCardNames(OutCardList);
                                NormalizeList(outCards);
                                if (outCards != null && outCards.Count > 0)
                                {
                                    for (int k = 0; k < outCards.Count; k++)
                                    {
                                        if (childportList != null && nameflowList != null)
                                        {
                                            this.hfCardsOutcoming.Value += "Luong:" + nameflowList[k] + "-Card:" + outCards[k] + "-Port:" + childportList[k] + "@";
                                        }
                                        else
                                        {
                                            this.hfCardsOutcoming.Value += "Luong:" + "-Card:" + outCards[k] + "-Port:" + "@";
                                        }
                                    }
                                }
                            }
                            //tram den
                            var outgoingStationIdList = this.DbContext.VIBA_LUONGs
                                        .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Den == cardIdList2[i] && x.Status == 1)
                                        .Select(x => x.Luong_Id_Tram_Den).Distinct().ToList();
                            NormalizeList(outgoingStationIdList);
                            if (outgoingStationIdList != null && outgoingStationIdList.Count > 0)
                            {
                                haveOutgoings = true;
                                var outStations = this.GetStationNames(outgoingStationIdList);
                                if (outStations != null && outStations.Count > 0)
                                {
                                    for (int j = 0; j < outStations.Count; j++)
                                        this.hfOutgoingStation.Value += outStations[j] + "@";                                    
                                }
                            }
                        }
                     }
                    this.hfDevices.Value = this.hfDevices.Value.Trim('|');
                    this.hfCards.Value = this.hfCards.Value.Trim('|');
                    this.hfCardsOutcoming.Value = this.hfCardsOutcoming.Value.Trim('|');
                    this.hfOutgoingStation.Value = this.hfOutgoingStation.Value.Trim('|');
                    }
            }            
        }

        protected void tree_NodeClick4(object sender, RadTreeNodeEventArgs e)
        {
            //clear all hidden fields
            this.hfStation.Value = string.Empty;
            this.hfDevices.Value = string.Empty;
            this.hfCards.Value = string.Empty;
            this.hfCardsIncoming.Value = string.Empty;
            this.hfCardsOutcoming.Value = string.Empty;
            this.hfIncomingStation.Value = string.Empty;
            this.hfOutgoingStation.Value = string.Empty;
            this.hflag.Value = "0";

            string stationId = e.Node.Value;
            var station = this.DbContext.VIBA_TRAMs.FirstOrDefault(x => x.Tram_Id == stationId);
            if (stationId != null)
            {
                this.hfStation.Value = station.Tram_Ten;
                //danh sach card gốc
                var cardIdList1 = this.DbContext.VIBA_LUONGs
                    .Where(x => x.Luong_Id_Tram_Den == stationId && x.Status == 1).Select(x => x.Luong_Card_Den).Distinct().ToList();
                //danh sach card ngọn
                var cardIdList2 = this.DbContext.VIBA_LUONGs
                    .Where(x => x.Luong_Id_Tram_Di == stationId && x.Status == 1).Select(x => x.Luong_Card_Di).Distinct().ToList();
                if (cardIdList1 != null && cardIdList1.Count>0 && cardIdList2 != null && cardIdList2.Count>0)
                {
                    hflag.Value = "2";
                    for (int i = 0; i < cardIdList1.Count; i++)
                    {
                        //card goc
                        var InComeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Di).Distinct().ToList();
                        //lay thong tin port goc
                        var parentportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Di).Distinct().ToList();
                        //card ngon của tram chon
                        var inCenterCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Den).Distinct().ToList();
                        // thong tin port trung tam(tram chon)
                        var InCenterPortList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Den).Distinct().ToList();
                        // thong tin luong trung tam (tram chon)
                        var InCenterFlowList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Ten).Distinct().ToList();
                        if (InComeCardList != null && InComeCardList.Count > 0)
                        {
                            //card trung tam
                            List<string> _CardList = new List<string>();
                            for (int j = 0; j < inCenterCardList.Count; j++)
                            {
                                _CardList.Add(inCenterCardList[j].ToString());
                            }
                            var CardList = this.GetCardNames(_CardList);
                            for (int j = 0; j < CardList.Count; j++)
                            {
                                this.hfCards.Value += "Card:" + CardList[j].ToString() + "-Port đến:" + InCenterPortList[j].ToString() + "@";
                            }
                            //card goc
                            List<string> _InComeCard = new List<string>();
                            for (int j = 0; j < InComeCardList.Count; j++)
                            {
                                _InComeCard.Add(InComeCardList[j].ToString());
                            }
                            var InComeCard = this.GetCardNames(_InComeCard);
                            NormalizeList(InComeCard);
                            for (int j = 0; j < InComeCard.Count; j++)
                            {
                                this.hfCardsIncoming.Value +="Luong:"+InCenterFlowList[j]+ "-Card:" + InComeCard[j].ToString() + "-Port:" + InCenterPortList[j].Value + "@";
                            }
                            var incomeStationIdList = this.DbContext.VIBA_LUONGs
                                        .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                                        .Select(x => x.Luong_Id_Tram_Di).Distinct().ToList();
                            NormalizeList(incomeStationIdList);
                            if (incomeStationIdList != null && incomeStationIdList.Count>0)
                            {
                                var InCenterStation = this.GetStationNames(incomeStationIdList);
                                for (int j = 0; j < InCenterStation.Count; j++)
                                {
                                	hfIncomingStation.Value +=InCenterStation[j]+"@";
                                }
                            }
                            this.hfCards.Value = this.hfCards.Value.Trim('|');
                            this.hfCardsIncoming.Value = this.hfCardsIncoming.Value.Trim('|');
                            this.hfIncomingStation.Value=this.hfIncomingStation.Value.Trim('|');
                        }
                    }
                    for (int i = 0; i < cardIdList2.Count; i++)
                    {
                        //card ngon
                        var outcomeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Den).Distinct().ToList();
                        //lay thong tin so port ngon
                        var childportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Den).Distinct().ToList();
                        //lay thong tin ten luong
                        var nameflowList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Ten).Distinct().ToList();
                        // card trung tam
                        var CardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Di).Distinct().ToList();
                        // port trung tam
                        var PortList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Di).Distinct().ToList();
                        // card trung tam
                        if (CardList != null && CardList.Count > 0)
                        {
                            List<string> _CardList = new List<string>();
                            for (int j = 0; j < CardList.Count; j++)
                            {
                                _CardList.Add(CardList[j].ToString());
                            }
                            var Card = this.GetCardNames(_CardList);
                            for (int j = 0; j < Card.Count; j++)
                            {
                                this.hfCards.Value += "Card:" + Card[j] + "-Port đi:" + PortList[j].Value + "@";
                            }
                        }                        
                        //card ngon
                        if (outcomeCardList != null && outcomeCardList.Count > 0)
                        {
                            List<string> _OutComeCard = new List<string>();
                            for (int j = 0; j < outcomeCardList.Count; j++)
                            {
                                _OutComeCard.Add(outcomeCardList[j].ToString());
                            }
                            var OutComeCard = this.GetCardNames(_OutComeCard);
                            for (int j = 0; j < OutComeCard.Count; j++)
                            {
                                this.hfCardsOutcoming.Value += "Card:" + OutComeCard[j].ToString() + "-Port đến:" + childportList[j].Value + "@";
                            }
                        }
                        //tram ngon
                        var outgoingStationIdList = this.DbContext.VIBA_LUONGs
                                        .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i] && x.Status == 1)
                                        .Select(x => x.Luong_Id_Tram_Den).Distinct().ToList();
                        NormalizeList(outgoingStationIdList);
                        var OutGoingStation = this.GetStationNames(outgoingStationIdList);
                        for (int j = 0; j < OutGoingStation.Count; j++)
                        {
                            this.hfOutgoingStation.Value += OutGoingStation[j] + "@";
                        }
                        this.hfCards.Value = this.hfCards.Value.Trim('|');
                        this.hfCardsOutcoming.Value = this.hfCardsOutcoming.Value.Trim('|');
                        this.hfOutgoingStation.Value = this.hfOutgoingStation.Value.Trim('|');
                    }                    
                }
                if (cardIdList1 != null && (cardIdList2 == null || cardIdList2.Count==0) && this.hflag.Value != "2")
                {
                    this.hflag.Value = "0";
                    for (int i = 0; i < cardIdList1.Count; i++)
                    {
                        //card goc
                        var incomeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Di).Distinct().ToList();
                        //lay thong tin so port goc
                        var inportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Di).Distinct().ToList();

                        if (incomeCardList != null && incomeCardList.Count > 0)
                        {
                            //card goc
                            List<string> _InComeCard = new List<string>();
                            for (int j = 0; j < incomeCardList.Count; j++)
                            {
                                _InComeCard.Add(incomeCardList[j].ToString());
                            }
                            var InComeCard = this.GetCardNames(_InComeCard);
                            for (int j = 0; j < InComeCard.Count; j++)
                            {
                                this.hfCardsIncoming.Value += "Card:" + InComeCard[j].ToString() + "-Port:" + inportList[j].ToString() + "@";
                            }
                            //tram goc
                            var incomeStationIdList = this.DbContext.VIBA_LUONGs
                                        .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                                        .Select(x => x.Luong_Id_Tram_Di).Distinct().ToList();
                            NormalizeList(incomeStationIdList);
                            var InComeStationName = this.GetStationNames(incomeStationIdList);
                            for (int j = 0; j < InComeStationName.Count; j++)
                            {
                                this.hfIncomingStation.Value += InComeStationName[j].ToString() + "@";
                            }
                        }
                        this.hfCardsIncoming.Value = this.hfCardsIncoming.Value.Trim('|');
                        this.hfIncomingStation.Value = this.hfIncomingStation.Value.Trim('|');
                        //card ngon
                        var outcomeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Den).Distinct().ToList();
                        //lay thong tin so port ngon
                        var outportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Den).Distinct().ToList();
                        //lay thong tin ten luong
                        var nameflowList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Den == stationId && x.Luong_Card_Den == cardIdList1[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Ten).Distinct().ToList();
                        if (outcomeCardList != null && outcomeCardList.Count > 0)
                        {
                            //card ngon
                            List<string> _OutComeCard = new List<string>();
                            for (int j = 0; j < outcomeCardList.Count; j++)
                            {
                                _OutComeCard.Add(outcomeCardList[j].ToString());
                            }
                            var OutComeCard = this.GetCardNames(_OutComeCard);
                            for (int j = 0; j < OutComeCard.Count; j++)
                            {
                                this.hfCardsOutcoming.Value += "Luong:" + nameflowList[j].ToString() + "-Card:" + OutComeCard[j].ToString() + "-Port:" + outportList[j].ToString()+"@";
                            }
                            
                        }
                        this.hfCardsOutcoming.Value = this.hfCardsOutcoming.Value.Trim('|');
                        //this.hfStation.Value = this.hfStation.Value.Trim('|');
                    }
                    
                }
                if (cardIdList2 != null && (cardIdList1 ==null || cardIdList1.Count ==0) && this.hflag.Value != "2")
                {
                    this.hflag.Value = "1"; // luong di tư tram chon 
                    for (int i = 0; i < cardIdList2.Count; i++)
                    {
                        //card goc
                        var incomeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Di).Distinct().ToList();
                        //lay thong tin so port goc
                        var inportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Di).ToList();

                        if (incomeCardList != null && incomeCardList.Count > 0)
                        {
                            //card goc
                            List<string> _InComeCard = new List<string>();
                            for (int j = 0; j < incomeCardList.Count; j++)
                            {
                                _InComeCard.Add(incomeCardList[j].ToString());
                            }
                            var InComeCard = this.GetCardNames(_InComeCard);
                            for (int j = 0; j < InComeCard.Count; j++)
                            {
                                if (inportList.Count >= j)
                                    this.hfCardsIncoming.Value += "Card:" + InComeCard[j].ToString() + "-Port:" + inportList[j].ToString() + "@";
                                else
                                    this.hfCardsIncoming.Value += "Card:" + InComeCard[j].ToString() + "@";
                                
                            }
                          
                        }
                        this.hfCardsIncoming.Value = this.hfCardsIncoming.Value.Trim('|');
                        //this.hfStation.Value = this.hfStation.Value.Trim('|');
                        //card ngon
                        var outcomeCardList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Card_Den).Distinct().ToList();
                        //lay thong tin so port ngon
                        var outportList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Port_Den).ToList();
                        //lay thong tin ten luong
                        var nameflowList = this.DbContext.VIBA_LUONGs
                            .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                            .Select(x => x.Luong_Ten).Distinct().ToList();
                        if (outcomeCardList != null && outcomeCardList.Count > 0)
                        {
                            //card ngon
                            List<string> _OutComeCard = new List<string>();
                            for (int j = 0; j < outcomeCardList.Count; j++)
                            {
                                _OutComeCard.Add(outcomeCardList[j].ToString());
                            }
                            var OutComeCard = this.GetCardNames(_OutComeCard);
                            for (int j = 0; j < OutComeCard.Count; j++)
                            {
                                if (outportList.Count>=j)
                                    this.hfCardsOutcoming.Value += "Luong:" + nameflowList[j].ToString() + "-Card:" + OutComeCard[j].ToString() + "-Port:" + outportList[j].ToString() + "@";
                                else 
                                    this.hfCardsOutcoming.Value += "Luong:" + nameflowList[j].ToString() + "-Card:" + OutComeCard[j].ToString() + "@";
                                
                            }
                            //tram ngon
                            var outcomeStationIdList = this.DbContext.VIBA_LUONGs
                                        .Where(x => x.Luong_Id_Tram_Di == stationId && x.Luong_Card_Di == cardIdList2[i].Value && x.Status == 1)
                                        .Select(x => x.Luong_Id_Tram_Den).Distinct().ToList();
                            NormalizeList(outcomeStationIdList);
                            var OutComeStationName = this.GetStationNames(outcomeStationIdList);
                            for (int j = 0; j < OutComeStationName.Count; j++)
                            {
                                this.hfOutgoingStation.Value += OutComeStationName[j].ToString() + "@";
                            }
                        }
                        this.hfCardsOutcoming.Value = this.hfCardsOutcoming.Value.Trim('|');
                        this.hfOutgoingStation.Value = this.hfOutgoingStation.Value.Trim('|');
                    }
                }
            }
        }

        private List<VIBA_CARD> cards;
        public List<VIBA_CARD> Cards
        {
            get {
                if (this.cards == null)
                {
                    using (var ctx = new Models.BTSModelDataContext()) {
                        this.cards = ctx.VIBA_CARDs.ToList();
                    }
                }

                return this.cards; 
            }
            set { this.cards = value; }
        }

        public List<VIBA_THIET_BI> Devices
        {
            get 
            {
                if (_devices == null)
                {
                    using (var ctx = new Models.BTSModelDataContext())
                        _devices = ctx.VIBA_THIET_BIs.ToList();
                }
                return _devices; 
            }
            set { _devices = value; }
        }
        private List<VIBA_THIET_BI> _devices;

        public List<VIBA_TRAM> Stations
        {
            get 
            {
                if (_stations == null)
                {
                    using (var ctx = new Models.BTSModelDataContext())
                        _stations = ctx.VIBA_TRAMs.ToList();
                }
                return _stations; 
            }
            set { _stations = value; }
        }
        private List<VIBA_TRAM> _stations;

        public string GetCardName(string cardId)
        {
            var first = this.Cards.FirstOrDefault(x => x.Card_Id.ToString() == cardId);
            if (first != null)
                return first.Card_TenCard;
            return string.Empty;
        }

        public int GetPortNumber(int cardId)
        {
            var first = this.Cards.FirstOrDefault(x => x.Card_Id == cardId);
            if (first != null)
                return first.Card_SoPort.Value;
            return -1000000000;
        }

        public string GetDeviceName(string deviceId)
        {
            var first = this.Devices.FirstOrDefault(x => x.ThietBi_Id.Trim() == deviceId.Trim());
            if (first != null)
                return first.ThietBi_Ten;
            return string.Empty;
        }

        public string GetDeviceName(int cardId)
        {
            var firstCard = this.Cards.FirstOrDefault(x => x.Card_Id == cardId);
            if (firstCard != null)
            {
                var firstDevice = this.Devices.FirstOrDefault(x => x.ThietBi_Id.Trim() == firstCard.ThietBi_Id.ToString());
                if (firstDevice != null)
                    return firstDevice.ThietBi_Ten;
            }
            return string.Empty;
        }

        public string GetStationName(string stationId)
        {
            var first = this.Stations.FirstOrDefault(x => x.Tram_Id.Trim() == stationId.Trim());
            if (first != null)
                return first.Tram_Ten;
            return string.Empty;
        }

        public void NormalizeList(List<string> list)
        {
            for (int i = 0; i < list.Count; i++)
                list[i] = list[i].Trim();
        }
        
        public List<string> GetStationNames(List<string> stationIds)
        {
            var list = this.Stations.Where(x => stationIds.Contains(x.Tram_Id)).Select(x => x.Tram_Ten).ToList();
            return list;
        }

        public List<string> GetCardNames(List<string> cardIds)
        {
            var list = this.Cards.Where(x => cardIds.Contains(x.Card_Id.ToString())).Select(x => x.Card_TenCard).ToList();
            return list;
        }

        private int GetSoPort(string deviceId)
        {
            var first = this.Cards.FirstOrDefault(x => x.ThietBi_Id == deviceId.Trim());
            if (first != null)
                return first.Card_SoPort != null ? first.Card_SoPort.Value : -1;
            return -1;
        }

        protected void btPrint_Click(object sender, EventArgs e)
        {
            var selNode = this.tree.SelectedNode;
            if (selNode != null)
            {
                Response.Redirect("FlowDiagramPrint.aspx?stationId=" + selNode.Value);
            }
            else
                Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Bạn phải chọn trạm muốn in');", true);
        }
    }
}